package org.hxd.employee.controller;

import org.hxd.employee.service.EmployeeService;
import org.hxd.pojo.Dept;
import org.hxd.pojo.Employee;
import org.hxd.pojo.Job;
import org.hxd.units.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class employeeController {
    @Autowired
    EmployeeService employeeService;

    @RequestMapping("/findemployee")
    public String findemployee(Model model, Employee employee, @RequestParam(defaultValue = "1") int pageIndex, Integer dept_id, Integer job_id) {
        if (job_id != null) {
            Job job = new Job();
            job.setId(job_id);
            employee.setJob(job);
        }
        if (dept_id != null) {
            Dept dept = new Dept();
            dept.setId(dept_id);
            employee.setDept(dept);
        }
        //职位下拉菜单
        List<Job> jobs = employeeService.findAllJob();
        model.addAttribute("jobs", jobs);
        //部门下拉菜单信息
        List<Dept> depts = employeeService.findAllDept();
        model.addAttribute("depts", depts);
        //记录数
        int recordCount = employeeService.findEmployeeCount(employee);
        PageModel pageModel = new PageModel();
        //pageModel.setPageSize(2);
        pageModel.setPageIndex(pageIndex);
        pageModel.setRocordCount(recordCount);
        model.addAttribute("pageModel", pageModel);
        List<Employee> employees = employeeService.findEmployee(employee, pageModel);
        model.addAttribute("employees", employees);
        model.addAttribute("employee", employee);
        return "jsp/employee/employee";
    }

    //员工修改按id查询
    @RequestMapping("/findEmployeeByid")
    public String findEmployeeByid(int id, Model model, int pageIndex) {
        model.addAttribute("pageIndex", pageIndex);
        //职位下拉菜单
        List<Job> jobs = employeeService.findAllJob();
        model.addAttribute("jobs", jobs);
        //部门下拉菜单信息
        List<Dept> depts = employeeService.findAllDept();
        model.addAttribute("depts", depts);
        Employee employee = employeeService.findEmployeeByid(id);
        model.addAttribute("employee", employee);
        return "jsp/employee/showUpdateEmployee";
    }

    //员工信息修改
    @RequestMapping("/modifyEmployee")
    @ResponseBody
    public String modifyEmployee(Employee employee) {
        int rows = employeeService.modifyEmployee(employee);
        if (rows > 0) {
            return "OK";
        } else {
            return "FAIL";
        }
    }

    //员工删除
    @RequestMapping("/removeEmployee")
    @ResponseBody
    public String removeEmployee(Integer[] ids) {
        int rows = employeeService.removeEmployee(ids);
        if (rows == ids.length) {
            return "OK";
        } else {
            return "FAIL";
        }
    }

    //进入添加员工页面
    @RequestMapping("/toAddEmployee")
    public String toAddEmployee(Model model){
        List<Job> jobs = employeeService.findAllJob();
        List<Dept> depts = employeeService.findAllDept();
        model.addAttribute("jobs",jobs);
        model.addAttribute("depts",depts);
        return "jsp/employee/showAddEmployee";
    }

    //添加员工
    @RequestMapping("/addEmployee")
    @ResponseBody
    public Object addEmployee(Employee employee){
        int rows = employeeService.addEmployee(employee);
        if(rows > 0){
            PageModel pageModel = new PageModel();
            int employeeCount = employeeService.findEmployeeCount(null);
            pageModel.setRocordCount(employeeCount);
            return pageModel.getTotalSize();
        }else {
            return "FAIL";
        }
    }
}
